package org.zjt.servletdemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zjt.servletdemo.domain.User;

import java.util.Map;


/**
 * DESC
 *
 * @author
 * @create 2017-08-28 下午3:07
 **/
@Service
public class UserService2 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void saveUser(User user) throws Exception {
        int update = jdbcTemplate.update("INSERT INTO user (`name`,`age`) VALUES (?,?)", preparedStatement2 -> {
            preparedStatement2.setString(1, user.getName());
            preparedStatement2.setInt(2, user.getAge());
        });
        new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
//        int i = 1 / 0;
        if (update < 1)
            throw new IllegalArgumentException("更新失败");
    }


}
